package me.seu.demo.tbox;

import java.util.ArrayList;
import java.util.List;

/**
 * 16进制数据分组
 *
 * @author: liangfeihu
 * @since: 2025-11-14 10:33
 */
public class HexDataGrouper {

    public static List<String> groupHexData(String hexData) {
        List<String> groups = new ArrayList<>();

        if (hexData == null || hexData.isEmpty()) {
            return groups;
        }

        // 查找所有"0001"出现的位置
        String delimiter = "0001";
        int currentIndex = 0;

        while (currentIndex < hexData.length()) {
            // 从当前位置开始查找下一个"0001"
            int nextGroupStart = hexData.indexOf(delimiter, currentIndex);

            if (nextGroupStart == -1) {
                // 没有找到更多的分组起始标记
                break;
            }

            // 如果是第一个分组，直接使用找到的位置
            if (groups.isEmpty()) {
                currentIndex = nextGroupStart;
            } else {
                // 对于后续分组，提取前一个分组的内容
                String group = hexData.substring(currentIndex, nextGroupStart);
                groups.add(group);
                currentIndex = nextGroupStart;
            }

            // 移动到下一个可能的位置
            if (currentIndex + 4 < hexData.length()) {
                // 继续查找下一个分组
            } else {
                break;
            }
        }

        // 添加最后一个分组
        if (currentIndex < hexData.length()) {
            String lastGroup = hexData.substring(currentIndex);
            groups.add(lastGroup);
        }

        return groups;
    }

    // 更简洁的版本，使用split方法
    public static List<String> groupHexDataSimple(String hexData) {
        List<String> groups = new ArrayList<>();

        if (hexData == null || hexData.isEmpty()) {
            return groups;
        }

        // 使用"0001"作为分隔符进行分割，但保留分隔符
        String[] parts = hexData.split("(?=0001)");

        for (String part : parts) {
            if (part.startsWith("0001")) {
                groups.add(part);
            }
        }

        return groups;
    }

    // 优化的版本，处理边界情况更好
    public static List<String> groupHexDataOptimized(String hexData) {
        List<String> groups = new ArrayList<>();

        if (hexData == null || hexData.isEmpty()) {
            return groups;
        }

        int index = 0;
        while (index < hexData.length()) {
            // 查找下一个"0001"的位置
            int nextStart = hexData.indexOf("0001", index);

            if (nextStart == -1) {
                // 没有找到更多的分组起始标记
                break;
            }

            // 查找当前分组的结束位置（下一个"0001"的位置或字符串末尾）
            int groupEnd = hexData.indexOf("0001", nextStart + 4);
            if (groupEnd == -1) {
                groupEnd = hexData.length();
            }

            // 提取分组
            String group = hexData.substring(nextStart, groupEnd);
            groups.add(group);

            // 移动到下一个位置
            index = groupEnd;
        }

        return groups;
    }

    public static void main(String[] args) {
        String str = "0001000068fff20600020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000ac58000000000000b0000000000000001000068fff20700020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000ac58000000000000b0000000000000001000068fff20800020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000ac58000000000000b0000000000000001000068fff20900020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000ac58000000000000b0000000000000001000068fff20a00020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000acc8000000000000b0000000000000001000068fff20b00020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000acc8000000000000b0000000000000001000068fff20c00020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000acc8000000000000b0000000000000001000068fff20d00020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000acc8000000000000b0000000000000001000068fff20e00020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000acc8000000000000b0000000000000001000068fff20f00020000008000000004138800000000000500000000006000060000000000000007000000000000000801cc000100000009593408123e1f000acc8000000000000b000000000000ffff0000e78bd005";

        // 使用优化的版本进行分组
        List<String> groups = groupHexDataOptimized(str);

        // 输出结果
        System.out.println("总共分组数: " + groups.size());
        for (int i = 0; i < groups.size(); i++) {
            System.out.println("分组 " + (i + 1) + " (长度: " + groups.get(i).length() + "):");
            System.out.println(groups.get(i));
            System.out.println("---");
        }
    }
}
